$(function(){
   
    var task = new Task();
    task.init().init();
    
});

function Task(){
    this.init = function(){
        return TASK;
    };
    
    var page = 10,  appCount = 0, subjectCount = 0, release = true, releaseNew = true;
    var TASK = {
        init : function(){
            TASK.funs.enabled();
            this.listeners.bindEvents();
        },
        listeners : {
            bindEvents : function(){
                $.util.foreachFunction(this, [ "bindEvents" ]);
            },
            bindStartLoadAppLink : function(){
                $("#btn_start_load_app_link").click(function(){
                    TASK.funs.disabled($("#btn_stop_load_app_link"));
                    var $this = $(this);
                    var num = 0;
                    var numAdd = 0;
                    $this.everyTime('30s', function(){
                        $.post(SYSTEM_DATA.contextPath + '/Task/writeUrl', {
                            'page' : page
                        }, function(jsonResult){
                            if(-1 == jsonResult.data){
                                $this.stopTime();
                                TASK.funs.enabled();
                                alert('距离抓取最新数据不足1小时 请稍候再试');
                                return;
                            }
                            page = jsonResult.data.page;
                            $("#span_load_app_link").text(num += 10);
                            
                            $("#span_load_app_link_add").text(numAdd += jsonResult.data.addCount);
                            //抓取完毕
                            if(-1 == page){
                                $this.stopTime();
                                alert('最新数据抓取完毕');
                                TASK.funs.enabled();
                            }
                        }, 'json');
                        
                    },0, true);
                });
            },
            bindStopLoadAppLink : function(){
                $("#btn_stop_load_app_link").click(function(){
                    $("#btn_start_load_app_link").stopTime();
                    TASK.funs.enabled();
                });
            },
            bindStartLoadApp : function(){
                $("#btn_start_load_app").click(function(){
                    TASK.funs.disabled($("#btn_stop_load_app"));
                    var $this = $(this);
                    $this.everyTime('15s', function(){
                        $.post(SYSTEM_DATA.contextPath + '/Task/releaseApp', {}, function(jsonResult){
                            release = jsonResult.data;
                            
                            if(release){
                                $("#span_load_app").text(appCount += 1);
                            }else{
                                $("#btn_stop_load_app").click();
                            }
                        }, 'json');
                    }, 0, true);
                });
            },
            bindStopLoadApp : function(){
                $("#btn_stop_load_app").click(function(){
                    $("#btn_start_load_app").stopTime();
                    TASK.funs.enabled();
                });
            },
            bindStartFreeApp : function(){
                $("#btn_start_free_app").click(function(){
                    TASK.funs.disabled($("#btn_stop_free_app"));
                    var $this = $(this);
                    $this.oneTime('2s', function(){
                        $.post(SYSTEM_DATA.contextPath + '/Task/releaseFreeAgain', {}, function(jsonResult){
                            if(jsonResult.data){
                                $('#span_free_app').text('数据抓取完毕');
                                alert('数据抓取完毕');
                                $("#btn_stop_free_app").click();
                            }
                        }, 'json');
                    });
                });
            },
            bindStopFreeApp : function(){
                $("#btn_stop_free_app").click(function(){
                    $("#btn_start_free_app").stopTime();
                    TASK.funs.enabled();
                });
            },
            bindStartLoadSubject : function(){
                $("#btn_start_load_subject").click(function(){
                    var $this = $(this);
                    
                    TASK.funs.disabled($("#btn_stop_load_subject"));
                    $this.oneTime('3s', function(){
                        $.post(SYSTEM_DATA.contextPath + '/Task/loadSubject.shtml', {}, function(jsonResult){
                            release = jsonResult.data;
                            if(jsonResult.status == 0){
                                $this.stopTime();
                                alert(jsonResult.info);
                            }else{
                                $("#span_load_subject").text(subjectCount += jsonResult.data);
                                $("#btn_stop_load_subject").click();
                                alert('专题数据抓取完毕');
                            }
                        }, 'json');
                    });
                });
            },
            bindStopLoadSubject : function(){
                $("#btn_stop_load_subject").click(function(){
                    $("#btn_start_load_subject").stopTime();
                    TASK.funs.enabled();
                });
                
            },
            bindStartLoadNewLink : function(){
                $("#btn_start_load_new_link").click(function(){
                    var newPage = 5;
                    var newCount = 0;
                    var count = 0;
                    var $this = $(this);
                    
                    TASK.funs.disabled($("#btn_stop_load_new_link"));
                    $this.everyTime('15s', function(){
                        $.post(SYSTEM_DATA.contextPath + '/Task/loadNewsHtml.shtml', {
                            'page' : newPage
                        }, function(jsonResult){
                            newPage = jsonResult.data.page;
                            if(0 == newPage){
                                $this.stopTime();
                                TASK.funs.enabled();
                                alert(jsonResult.info + '  可以抓取最新的新闻了');
                            }
                            else{
                                $("#span_load_new_link").text(count += 30);
                                $("#span_load_new_link_add").text(newCount += jsonResult.data.count);
                            }
                        }, 'json');
                    }, 0, true);
                });
            },
            bindStopLoadNewLink : function(){
                $("#btn_stop_load_new_link").click(function(){
                    $("#btn_start_load_new_link").stopTime();
                    TASK.funs.enabled();
                });
            },
            bindStartLoadNew : function(){
                $("#btn_start_load_new").click(function(){
                    var newCount = 0;
                    var $this = $(this);
                    
                    TASK.funs.disabled($("#btn_stop_load_new"));
                    $this.everyTime('5s', function(){
                        $.post(SYSTEM_DATA.contextPath + '/Task/releaseNews.shtml', {
                            
                            }, function(jsonResult){
                                if(!jsonResult.data){
                                    TASK.funs.removeCache('/Cache/removeNewsCache.shtml');
                                    $this.stopTime();
                                    TASK.funs.enabled();
                                    alert(jsonResult.info);
                                }
                                else{
                                    $("#span_load_new").text(newCount += 1);
                                }
                            }, 'json');
                    }, 0, true);
                });
            },
            bindStopLoadNew : function(){
                $("#btn_stop_load_new").click(function(){
                    $("#btn_start_load_new").stopTime();
                    TASK.funs.enabled();
                });
            },
            bindTopStart : function(){
                $("#btn_start_top").click(function(){
                    var $this = $(this);
                    TASK.funs.disabled($("#btn_stop_top"));
                    $this.oneTime('1s', function(){
                        $.post(SYSTEM_DATA.contextPath + '/Task/releaseTop.shtml', {
                           
                            }, function(jsonResult){
                                if(jsonResult.data){
                                    $this.stopTime();
                                    TASK.funs.enabled();
                                    $("#span_top").text('最新排行榜已经生成');
                                    alert('最新排行榜已经生成');
                                }
                                else{
                                    $this.stopTime();
                                    TASK.funs.enabled();
                                    alert('今日已经生成过最新排行榜');
                                }
                            }, 'json');
                    });
                });
            },
            bindTopStop : function(){
                $("#btn_stop_top").click(function(){
                    TASK.funs.enabled();
                });
            }
        },
        funs : {
            disabled : function($element){
                $("#table_main").find(':button').filter(function(){
                    return $(this).attr('id') != $element.attr('id');
                }).attr('disabled', 'disabled');
            },
            enabled : function(){
                $("#table_main").find(':button').removeAttr('disabled');
            },
            removeCache : function(requestURL){
                $.post(SYSTEM_DATA.contextPath + requestURL, {}, function(){}, "json");
            }
        }
    };
}


 
function loadHtml(){
    
}
    
function releaseApp(){
    if(release){
        
        setTimeout("releaseApp()",1000 * 20);
    }
}
        
function loadNewsHtml(){
    $.post(contextPath + '/Task/loadNewsHtml', {
        'page' : htmlPage
    }, function(jsonResult){
        //                    console.debug(jsonResult.data);

        htmlPage = jsonResult.data;
    }, 'json');
    setTimeout("loadNewsHtml()",1000 * 20);
}
        
function releaseNews(){
    if(releaseNew){
        $.post(contextPath + '/Task/releaseNews', {}, function(jsonResult){
            //                    console.debug(jsonResult.data);
            releaseNew = jsonResult.data;
        }, 'json');
        setTimeout("releaseNews()",1000 * 30);
    }
}
function releaseSubjectContent(){
  
    $.post(contextPath + '/Task/releaseSubjectContent', {}, function(jsonResult){
        console.debug(jsonResult.data);
    //        releaseNew = jsonResult.data;
    }, 'json');
    
    setTimeout("releaseSubjectContent()",1000 * 15);
    
}
function releaseTop(){
  
    $.post(contextPath + '/Task/releaseTop', {}, function(jsonResult){
        //                    console.debug(jsonResult.data);
        //        releaseNew = jsonResult.data;
        }, 'json');
    
}

function releaseFreeAgain(){

    $.post(contextPath + '/Task/releaseFreeAgain', {}, function(jsonResult){
        //                    console.debug(jsonResult.data);
        releaseFreeAgain = jsonResult.data;
    }, 'json');
        
    
}




